const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceProduct = require("../../../service/service_product.js");
const ServiceScore = require("../../../service/service_score.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    allScore: 0,
    /**
     * 产品
     * 数据格式：
        [
          { id: 1, name: "法国进口红酒拉斐天使庄园干红葡萄酒红酒整箱礼盒装750ml*6", price: 100,
            imgUrl: 'xxx.jpg',
            properties: [
              { id: 1, name: "葡萄酒" },
              { id: 2, name: "干红" },
              { id: 3, name: "750ml" },
              { id: 4, name: "法国" },
              { id: 5, name: "6只装" }
            ]
          }
        ]
    */
    products: [],
    /**
     * 过滤属性
     * 数据格式：
        [
          { id: 1, name: "品牌",
            items: [{ id: 1, name: "a品牌" }, { id: 2, name: "b品牌" }]
          },
          { id: 2, name: "产地",
            items: [{ id: 3, name: "a产地" }, { id: 4, name: "b产地" }]
          },
          { id: 3, name: "容量",
            items: [{ id: 5, name: "a容量" }, { id: 6, name: "b容量" }]
          }
        ]
    */
    properties: [],
    /**
     * 开始页
     */
    filter: [],
    startPage: 0,
    types: ['礼品包装'],
    /**
     * 选择酒相关
     */
    wineShowFlag: false,
    wines: [],
    selectWine: {},
    searchProductName: null,
    /**
     * 购物车相关
     */
    triggerAddData: null,
    triggerRemoveData: null,
    triggerRetrieveData: null,
    car_amount: 0,
    allDataMap: {}
  },

  methods: {
    // const data = {
    //   id: 0,
    //   name: "法国进口红酒 拉菲（LAFITE）传奇波尔多干红葡萄酒750ml*6整箱装",
    //   price: "200",
    //   imgUrl: "https://img12.360buyimg.com/n7/jfs/t1/81294/28/12887/117044/5da52901E45086cec/4919e55b8baca1b7.jpg",
    //   unit: "箱（1*6）",
    //   describe: "该产品起订量为10000元，因订单量大，快递延缓有序发货，着急用酒者谨慎下单。"
    // };
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      // 加载数据
      ServiceScore.find(function (data) {
        _this.setData({
          allScore: data
        })
      })
      // 列出产品
      ServiceProduct.pageByWineId(0, _this.data.types, [], _this.data.selectWine.id, function (data) {
        _this.setData({ products: data.records });
        const categoryId = data.data;
        // 列出产品分类对应的属性
        ServiceProduct.listProperties(categoryId, function (data) {
          _this.setData({ properties: data.records });
        });
      });
    },
    /**
     * onShow
     */
    onShow: function (e) {
      const _this = this;
      _this.setData({ triggerRetrieveData: true });
    },
    /**
     * 添加到购物车
     */
    onAdd: function (e) {
      const _this = this;
      const productId = e.currentTarget.dataset.id;
      ServiceProduct.findById(productId, function (data) {
        _this.setData({
          triggerAddData: {
            id: data.id,
            name: data.name,
            value: data.price,
            imgUrl: data.imgUrl,
            amount: 1,
            selected: true,
            describe: data.comment
          }
        })
      });
    },
    /**
     * 从购物车移除
     */
    onRemove: function (e) {
      const _this = this;
      const productId = e.currentTarget.dataset.id;
      _this.setData({
        triggerRemoveData: { id: productId }
      })
    },
    /**
     * 数据改变
     */
    car_onChange: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        allDataMap: e.detail.allDataMap
      })
    },
    /**
     * 获取数据
     */
    car_onRetrieve: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        allDataMap: e.detail.allDataMap
      })
    },
    /**
     * 显示酒
     */
    onShowSelectProduct: function(e){
      const _this = this;
      const name = _this.data.searchProductName;
      ServiceProduct.list(['红酒'], [], { name }, function (data) {
        _this.setData({
          wineShowFlag: true,
          wines: data.records
        });
        // const categoryId = data.data;
        // // 列出产品分类对应的属性
        // ServiceProduct.listProperties(categoryId, function (data) {
        //   _this.setData({ properties: data.records });
        // });
      });
    },
    /**
    * 选择酒
    */
    onSelectProduct: function (e) {
      const _this = this;
      let selectWine = e.currentTarget.dataset.wine;
      selectWine = null === selectWine ? {} : selectWine;
      // 列出产品
      ServiceProduct.pageByWineId(0, _this.data.types, [], selectWine.id, function (data) {
        _this.setData({ 
          products: data.records,
          selectWine,
          wineShowFlag: false
        });
      });
    },
    /**
     * 改变搜索产品名称
     */
    onChangeSearchProductName: function(e){
      const _this = this;
      const value = e.detail.value;
      _this.setData({
        searchProductName: value
      });
    },
    /**
     * 搜索产品名称
     */
    onSearchProduct: function (e) {
      const _this = this;
      const name = _this.data.searchProductName;
      ServiceProduct.list(['红酒'], [], { name }, function (data) {
        _this.setData({
          wines: data.records
        });
      });
    },
    /**
      * 关闭选择酒列表
      */
    onCloseSelectProductList: function(e){
      const _this = this;
      _this.setData({
        wineShowFlag: false
      });
    },
    /**
     * 触底事件
     */
    onReachBottom: function () {
      const _this = this;
      let startPage = this.data.startPage + 1;
      ServiceProduct.pageByWineId(startPage, _this.data.types, [], _this.data.selectWine.id, function (data) {
        _this.data.products.push(...data.records);
        _this.setData({
          products: _this.data.products,
          startPage: startPage
        });
      });
    }
  }

})